package com.atguigu.es.client;

import com.atguigu.es.beans.Emp;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.AvgAggregation;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.List;

/**
 * Created by Smexy on 2022/5/21
 *
 *
 *
 *       Action: 要执行的命令
 *              读: Search
 *
 *
 */
public class ReadDemo2 {

    public static void main(String[] args) throws IOException {

        //创建客户端
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 连接server端
        HttpClientConfig httpClientConfig = (new HttpClientConfig.Builder("http://hadoop102:9200")).build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        JestClient jestClient = jestClientFactory.getObject();


        // 构造match:{}
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("hobby", "购物");

        //构造 gendercount:{}
        TermsAggregationBuilder agg2 = AggregationBuilders.terms("gendercount").field("gender.keyword").size(2);

        //构造 agecount:{}
        AvgAggregationBuilder ageCount = AggregationBuilders.avg("avgage").field("age");

        //构造最外层{}
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(matchQueryBuilder).aggregation(agg2).aggregation(ageCount);

         //由一个对象生成，而不是字符串的拼接
         String requestBody=searchSourceBuilder.toString();
        // 准备操作的命令
        Search search = new Search.Builder(requestBody).build();


        // 执行命令
        SearchResult searchResult = jestClient.execute(search);

        // 如果是读，解析server返回的结果
        System.out.println("total:"+searchResult.getTotal());
        System.out.println("max_score:"+searchResult.getMaxScore());

        List<SearchResult.Hit<Emp, Void>> hits = searchResult.getHits(Emp.class);

        for (SearchResult.Hit<Emp, Void> hit : hits) {

            System.out.println("_index"+hit.index);
            System.out.println("_type"+hit.type);
            System.out.println("_id"+hit.id);
            System.out.println("_score"+hit.score);
            System.out.println(hit.source);

        }

        //获取聚合
        MetricAggregation aggregations = searchResult.getAggregations();

        TermsAggregation gendercount = aggregations.getTermsAggregation("gendercount");

        for (TermsAggregation.Entry bucket : gendercount.getBuckets()) {

            System.out.println(bucket.getKey() + ":" + bucket.getCount());

        }

        AvgAggregation avgage = aggregations.getAvgAggregation("avgage");

        System.out.println(avgage.getAvg());

        // 关闭客户端
        jestClient.close();

    }


}
